<?php
namespace app\courier\controller;

use app\common\controller\Api;
use think\Db;

class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 默认方法
    public function _initialize()
    {
        parent::_initialize();

    }

    // 首页信息
    public function index()
    {

        $this->success('请求成功');

    }

    // 公告列表
    public function notice_list()
    {

        // 配送员id
        $courier_id = $this->request->get('courier_id',0,'intval');

        // 获取配送员信息
        $notice_list = Db::name('notice')->where(array('courier_id'=>$courier_id))->select();

        $this->success('请求成功', $notice_list);

    }

    // 配送员信息
    public function courier_info()
    {

        // 获取配送员信息
        $courier_info = Db::name('courier')->where(array('userid'=>$this->auth->id))->find();

        $this->success('请求成功', $courier_info);

    }

    // 设置开工状态
    public function set_operative_mode()
    {

        // 接收开工状态
        $operative_mode = $this->request->request('operative_mode',0,'intval');

        // 获取配送员信息
        $result = Db::name('courier')->where(array('userid'=>$this->auth->id))->update(array('operative_mode'=>$operative_mode));

        if($result !== false){

            $this->success('设置成功');

        }else{

            $this->error('设置失败');

        }

    }



    // 评价列表
    public function comment_list()
    {

        // 配送员id
        $courier_id = $this->request->get('courier_id',0,'intval');

        // 获取配送员评价列表
        $notice_list = Db::name('courier_comment')->where(array('courier_id'=>$courier_id,'status'=>'normal'))->select();

        $this->success('请求成功', $notice_list);

    }


    // 配送统计
    public function express_statistics()
    {

        // 根据登录用户id 查询 配送员id
        $courier_id = Db::name('courier')->where(array('userid'=>$this->auth->id))->value('id');

        // 定义数组
        $data = array();

        // 今天营业额
        $today_money = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime','today')->sum('money');
        $data['today_money'] = number_format($today_money,2, ".", "");
        $data['today_number'] = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime','today')->count();

        // 近7日营业额
        $seven_money = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime','-7 day')->sum('money');
        $data['seven_money'] = number_format($seven_money,2, ".", "");
        $data['seven_number'] = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime','-7 day')->count();

        // 近30日营业额
        $thirty_money = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime','-30 day')->sum('money');
        $data['thirty_money'] = number_format($thirty_money,2, ".", "");
        $data['thirty_number'] = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime','-30 day')->count();

        // 自定义查询
        $start_day = $this->request->get('start_day',false);
        $end_day = $this->request->get('end_day',false);
        if($start_day && $end_day){

            $search_money = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime', 'between', [$start_day, $end_day])->sum('money');

            $data['search_money'] = number_format($search_money,2, ".", "");
            $data['search_number'] = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime', 'between', [$start_day, $end_day])->count();

        }

        $this->success('请求成功', $data);

    }


    // 我的账户
    public function express_account()
    {

        // 根据登录用户id 查询 配送员id
        $courier_id = Db::name('courier')->where(array('userid'=>$this->auth->id))->value('id');

        // 定义数组
        $data = array();

        // 总收益
        $total_money = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->sum('money');
        $data['total_money'] = number_format($total_money,2, ".", "");

        // 今日收益
        $today_money = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime','today')->sum('money');
        $data['today_money'] = number_format($today_money,2, ".", "");

        // 昨日收益
        $yesterday_money = Db::name('courier_balance')->where(array('courier_id'=>$courier_id,'type'=>'add'))->whereTime('createtime','yesterday')->sum('money');
        $data['yesterday_money'] = number_format($yesterday_money,2, ".", "");

        // 收益记录（配送费）
        $data['express_list'] = Db::name('courier_balance')->where(array('courier_id'=>$courier_id))->select();

        $this->success('请求成功', $data);

    }

}
